

// 全局设置axios
axios.defaults.baseURL = 'http://localhost:8080/';
Vue.prototype.$http = axios;

// 解析url参数函数
function parseParamObj(url) {
    let paramsStr = url.substring(url.indexOf("?") + 1);
    let paramsArray = paramsStr.split("&");
    let paramObj = {};
    $.each(paramsArray, function (index, param) {
        let parmName = param.split("=")[0];
        let parmValue = param.split("=")[1];
        paramObj[parmName] = parmValue;
    })
    return paramObj;
}

// 配置请求拦截
let excludeUrls = ['/index.html', "/about.html"];
let url = location.href;
let isPermitted = false;
if (url != "http://localhost:8082/") //首页特殊地址单独放行
{
    for (let i = 0; i < excludeUrls.length; i++) {
        if (url.indexOf(excludeUrls[i]) != -1) {
            //放行
            isPermitted = true;
            break;
        }
    }
}
else { 
    isPermitted = true;
}

if (!isPermitted) //拦截
{
    let user = localStorage.getItem("loginInfo");
    if (!user)
        alert("您还没有登录！请登录后再操作")
}

// 配置所有请求携带令牌到后端
axios.interceptors.request.use(config=>{
    let token = localStorage.getItem("token")
    if(token){
        config.headers["token"] = token;
    }
    return config;
},error => {
    Promise.reject(error)
})

 // 前端拦截
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success && "noLogin"===data.message)
    {
        localStorage.removeItem("token");
        localStorage.removeItem("user");
        alert("你还没有登录，请登录后再操作！")
    }
    return result;
},error => {
    Promise.reject(error)
})